function Notification() {
	this.isActivated = false;
	this.isInitialized = false;
	this.notification = null;
	this.currentTerm;
	this.win;
	this.nt = 0;
	
	this.show = function(url) {
		if (!this.isInitialized) 
			this._init();
		
		if (this.isActivated) {
			return false;
		}		
		
		//if (!this.notification) {
			this.notification = webkitNotifications.createHTMLNotification(url);
			this.notification.onclose = this._closeEvent.bind(this);
		//}
			
		this.isActivated = true;		
		this.notification.show();
		
		return true;
	};
	
	this._init = function() {	
		// Test for notification support.
		if (!webkitNotifications) {
			throw "No support for notifications";
		}
		
		this.isInitialized = true; // The option initialization.		
		this.isActivated = false;
		this.currentTerm = getRandomTerm();
		
		//window.setInterval(this.show.bind(this), this.frequency);
	};
	
	this._closeEvent = function() {
		this.isActivated = false;
		$(document).stopTime('_termWindowAnimate');		
		$(document).oneTime(optionsManager.getNotificationFrequency()*1000, 'termWindowShow', this.termWindowShow.bind(this));
	};
	
	/**
	* callback function for timer. This function animate timer and when timer is over
	* show window of translate
	*/
	this._termWindowAnimate = function () {
		chrome.extension.getViews({type:"notification"}).forEach(function(win) {						
			this.win = win;
			var timerObject = win.document.getElementById('timer');
			if (timerObject) {	// i am paranoic
			
				if (timerObject.innerHTML == '') {
					timerObject.innerHTML = optionsManager.getNotificationDelay() + 1;					
				}
			
				if (timerObject.innerHTML <= 1) {
					this._translateWindowInit(win);
					timerObject.innerHTML = optionsManager.getNotificationDelay() + 1;
				} else {
					if (timerObject.innerHTML == optionsManager.getNotificationDelay()) {
						// cranches her :-(
						$(win.document.getElementById('timer')).show();
						$(win.document.getElementById('term_choiser')).hide();
						if (optionsManager.getNotificationType() == 0) {
							win.document.getElementById('translate_value').innerHTML = getRandomDescription(this.currentTerm);							
						} else {
							win.document.getElementById('translate_value').innerHTML = this.currentTerm.getRandomDescription();
						}
						this.currentTerm.repeatCount = this.currentTerm.repeatCount + 1;
					}					
					
					timerObject.innerHTML = timerObject.innerHTML - 1;
				}
			}			
		}.bind(this));
	};
	
	this._hideResult = function() {
		this.notification.cancel();
		this.isActivated = false;
		
		$(this.win.document.getElementById('term_window')).show();
		$(this.win.document.getElementById('result_good')).hide();
		$(this.win.document.getElementById('result_bad')).hide();
		
		this.currentTerm = getRandomTerm();	
		$(document).oneTime(optionsManager.getNotificationFrequency()*1000, 'termWindowShow', this.termWindowShow.bind(this));			
	};
	
	this._showResult = function(win, isOk) {
		$(win.document.getElementById('term_window')).hide();
		
		if (isOk) {
			$(win.document.getElementById('result_good')).show();
			$(document).oneTime(1000, this._hideResult.bind(this));
		} else {
			$(win.document.getElementById('result_term_value')).html(this.currentTerm.value);			
			//$(win.document.getElementById('result_term_translates')).html(this.currentTerm.allDescsToString());
					
			$(win.document.getElementById('result_bad')).show();	
			$(document).oneTime(3000, this._hideResult.bind(this));
		}				
			
	};
	
	this._checkTermMeaning = function(win, answer) {		
		$(win.document.getElementById('timer')).show();
		$(win.document.getElementById('term_choiser')).hide();
		$(win.document.getElementById('term_value')).hide();				
		$(win.document.getElementById('button_box')).hide();	
		$(win.document.getElementById('notification_term')).hide();			
				
		var isOk = false;
		
		if (answer == 0) {
			this._hideResult();
			return;
		}
		
		if (this.nt == 0) {
		var currentValue = $(win.document.getElementById('translate_value')).html();
		// window hold correct answer for current window
		if (this.currentTerm.haveDescription (currentValue)) {
			if (answer == 1) {
				this.currentTerm.successCount = this.currentTerm.successCount + 1;
				isOk = true;
			} else {
				if (this.currentTerm.successCount > 0) {
					this.currentTerm.successCount = this.currentTerm.successCount - 1;
				}
			}
		} else {
			if (answer == -1) {
				this.currentTerm.successCount = this.currentTerm.successCount + 1;
				isOk = true;
			} else {
				if (this.currentTerm.successCount > 0) {
					this.currentTerm.successCount = this.currentTerm.successCount - 1;
				}
			}
		}
		
		} else {
			var currentValue = $(win.document.getElementById('term_value_input')).val();
			
			if (this.currentTerm.value == currentValue) {
				isOk = true;
				this.currentTerm.successCount = this.currentTerm.successCount + 1;
			} else {
				if (this.currentTerm.successCount > 0) {
					this.currentTerm.successCount = this.currentTerm.successCount - 1;
				}
			}
		}
		
		Voc.updateTerm(this.currentTerm);
		
		this._showResult(win, isOk);		
	};
	
	this._translateWindowInit = function () {
		var win = this.win;		
		
		$(document).stopTime('_termWindowAnimate');
		this.nt = optionsManager.getNotificationType();
		
		$(win.document.getElementById('term_choiser')).show();
		if (!optionsManager.getNotificationType()) {			
			win.document.getElementById('term_value').innerHTML = this.currentTerm.value;
			$(win.document.getElementById('term_value')).show();				
			$(win.document.getElementById('button_box')).show();	
			$(win.document.getElementById('notification_term')).hide();			
		} else {
			$(win.document.getElementById('term_value')).hide();	
			$(win.document.getElementById('button_box')).hide();
			$(win.document.getElementById('notification_term')).show();				 
						
			$(win.document.getElementById('check_div')).click(function() { this._checkTermMeaning(win, 1); }.bind(this));			
		}		
		
		$(win.document.getElementById('timer')).hide();
		
		$(win.document.getElementById('button_yes')).click(function() { this._checkTermMeaning(win, 1); }.bind(this));
		$(win.document.getElementById('button_maybe')).click(function() { this._checkTermMeaning(win, 0); }.bind(this));
		$(win.document.getElementById('button_no')).click(function() { this._checkTermMeaning(win, -1); }.bind(this));
	};
	
	/**
	* initialize term window popup and start animation of timer.
	*/
	this.termWindowShow = function () {		
		this.show('/html/notification_term.html');	
		$(document).stopTime('_termWindowAnimate');		
		$(document).everyTime(1000, '_termWindowAnimate', this._termWindowAnimate.bind(this));
	};
	
	$(document).oneTime(optionsManager.getNotificationFrequency()*1000, this.termWindowShow.bind(this));
}

onload = function() {
	var notificationWindow = new Notification();
}
